package com.hermes.action.homepage;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hermes.biz.common.model.item.ItemUserRelationDO;
import com.hermes.biz.common.model.message.MessageInfoDO;
import com.hermes.biz.common.model.message.UserMessageInfoDO;
import com.hermes.biz.common.model.result.ItemResultDO;
import com.hermes.biz.common.model.result.UserResultDO;
import com.hermes.biz.common.util.string.StringUtil;
import com.hermes.biz.service.item.ItemService;
import com.hermes.biz.service.item.UserItemRelationService;
import com.hermes.biz.service.user.UserMessageService;
import com.hermes.biz.service.user.UserService;
import com.hermes.common.action.BaseAction;
import com.hermes.common.util.LoginHelperUtil;
import com.hermes.common.util.ParameterParserUtil;

/**
 * Message-List
 * 
 *
 */
public class UserMessageList extends BaseAction {

    private static final long       serialVersionUID  = 1L;

    private Logger                  log               = Logger.getLogger(MessageListCenter.class);

    private UserMessageService      userMessageService;

    private UserItemRelationService userItemRelationService;

    private ItemService             itemService;

    private UserService             userService;

    public String                   username          = "";

    public boolean                  isLogin           = false;

    public long                     userid;

    public int                      interItemCount;

    public List<UserMessageInfoDO>  messageResultList = new ArrayList<UserMessageInfoDO>();

    @Override
    public String execute() {

        //If-Login
        HttpServletRequest request = ServletActionContext.getRequest();
        LoginHelperUtil loginHelper = new LoginHelperUtil(request);
        if (loginHelper.hasLogined()) {
            setLogin(true);
        }

        String userIdStr = ParameterParserUtil.getParameter("userId");
        String msgType = ParameterParserUtil.getParameter("msgt");

        if (StringUtil.isNotBlank(userIdStr) || StringUtil.isNotBlank(msgType)) {
            return SUCCESS;
        }

        long userId = 0;
        int msgTypeNum = 0;
        if (StringUtil.isNumOnly(userIdStr)) {
            userId = Long.parseLong(userIdStr);

            List<ItemUserRelationDO> itemList = userItemRelationService
                .getUserInterestedItems(userId);

            setInterItemCount(itemList == null ? 0 : itemList.size());

            msgTypeNum = Integer.parseInt(msgType);
            List<MessageInfoDO> resultList = userMessageService.getUserMessageByType(userId,
                msgTypeNum);

            if (resultList == null || resultList.size() < 1) {
                return SUCCESS;
            }

            for (MessageInfoDO mid : resultList) {
                UserMessageInfoDO userMid = new UserMessageInfoDO();
                userMid.setFriendId(mid.getUserId());
                UserResultDO userRD = userService.getUserInfoByUserId(userMid.getFriendId());
                if (userRD.isSuccess() && userRD.getModule() != null) {
                    userMid.setFriendName(userRD.getModule().getUserName());
                    userMid.setFriendPic(userRD.getModule().getHeadPic());
                } else {
                    continue;
                }
                userMid.setUserId(userId);
                userMid.setGmtCreate(mid.getGmtCreate());
                userMid.setItemId(mid.getItemId());
                ItemResultDO itemRD = itemService.getSingleItemByItemId(mid.getItemId());
                if (itemRD.isSuccess() && itemRD.getModule() != null) {
                    userMid.setItemTitle(itemRD.getModule().getItemTitle());
                    userMid.setItemPic(itemRD.getModule().getItemPicUrl());
                } else {
                    continue;
                }
                userMid.setMessageContent(mid.getMessageContent());
                userMid.setOriginalMessageId(mid.getOriginalMessageId());
                userMid.setOriginalMessageContent(mid.getOriginalMessageContent());

                messageResultList.add(userMid);

            }

            return SUCCESS;
        }
        log.error("");
        return SUCCESS;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public boolean isLogin() {
        return isLogin;
    }

    public void setLogin(boolean isLogin) {
        this.isLogin = isLogin;
    }

    public long getUserid() {
        return userid;
    }

    public void setUserid(long userid) {
        this.userid = userid;
    }

    public UserMessageService getUserMessageService() {
        return userMessageService;
    }

    public void setUserMessageService(UserMessageService userMessageService) {
        this.userMessageService = userMessageService;
    }

    public ItemService getItemService() {
        return itemService;
    }

    public void setItemService(ItemService itemService) {
        this.itemService = itemService;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public List<UserMessageInfoDO> getMessageResultList() {
        return messageResultList;
    }

    public void setMessageResultList(List<UserMessageInfoDO> messageResultList) {
        this.messageResultList = messageResultList;
    }

    public UserItemRelationService getUserItemRelationService() {
        return userItemRelationService;
    }

    public void setUserItemRelationService(UserItemRelationService userItemRelationService) {
        this.userItemRelationService = userItemRelationService;
    }

    public int getInterItemCount() {
        return interItemCount;
    }

    public void setInterItemCount(int interItemCount) {
        this.interItemCount = interItemCount;
    }

}